var lottery = require('./code/lottery/lottery');
var path = require("path");
var query = require("./db/db");

console.log('抓取间隔时间为20秒,程序已成功启动');
function pk10day() {
    function getLocalTime(i) {
        //参数i为时区值数字，比如北京为东八区则输进8,西5输入-5
        if (typeof i !== 'number') return;
        var d = new Date();
        //得到1970年一月一日到现在的秒数
        var len = d.getTime();
        //本地时间与GMT时间的时间偏移差
        var offset = d.getTimezoneOffset() * 60000;
        //得到现在的格林尼治时间
        var utcTime = len + offset;
        return new Date(utcTime + 3600000 * i);
    }

    var date = getLocalTime(-9);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    if (month < 10) {
        month = "0" + month;
    }
    var day = date.getDate();
    if (day < 10) {
        day = "0" + day;
    }
    var todaystr = month + '%2F' + day + '%2F' + year;
    var todaystr1 = year + '-' + month + '-' + day;

    function realtime(b) {
        var stopday = 0;
        var thattime = '2016-06-01 18:30';//计算标准
        var thatid = 77874;
        var plustime;
        if (b - thatid > 264) {
            xx = Math.floor((b - thatid) / 265) + stopday;//过了多少天
            yy = (b - thatid) % 265;             //当天的第几期
            plustime = yy * 5 + (60 * 24) * xx;
        } else {
            plustime = (b - 77874) * 5;
        }
        var time = new Date(thattime.replace("-", "/"));
        time.setMinutes(time.getMinutes() + plustime, time.getSeconds(), 0);
        var year = time.getFullYear().toString();
        var month = (time.getMonth() + 1).toString();
        if (month < 10) {
            month = '0' + month
        }
        var day = time.getDate().toString();
        if (day < 10) {
            day = '0' + day
        }
        var hour = time.getHours().toString();
        if (hour < 10) {
            hour = '0' + hour
        }
        var mins = time.getMinutes().toString();
        if (mins < 10) {
            mins = '0' + mins
        }
        time = year + '-' + month + '-' + day + ' ' + hour + ':' + mins;
        return (time)
    }

    console.log('抓取' + todaystr1 + '最新一期');
    lottery.getdate.jndkeno(todaystr, function (json_data) {
        if (json_data.length && json_data != 'empty' && json_data != 'error') {
            var todaystr2 = new Date();
            console.log('数据加载完成');
            console.log('目前最新期为:' + json_data[json_data.length - 1].gamenum);
            var data = json_data[json_data.length - 1];
            //对ballnum处理为数组
            var rtotal = 0;
            for (var i = 1; i < 21; i++) {
                rtotal += parseInt(data['r' + i]);
            }
            console.log(rtotal);
            //查询id是否存在
            var sql1 = 'select * from result where gameid = ' + data.gamenum + ' and gamekind = "jndkeno";';
            //写入数据
            var sql2 = 'insert app.result(gamekind,gameid,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,rtotal,daystr,opentime) values("jndkeno"' +
                ',"' + data.gamenum + '","' + data.r1 + '","' + data.r2 + '","' + data.r3 + '","' + data.r4 +
                '","' + data.r5 + '","' + data.r6 + '","' + data.r7 + '","' + data.r8 + '","' + data.r9 +
                '","' + data.r10 + '","' + data.r11 + '","' + data.r12 + '","' + data.r13 + '","' + data.r14 +
                '","' + data.r15 + '","' + data.r16 + '","' + data.r17 + '","' + data.r18 + '","' + data.r19 +
                '","' + data.r20 + '","' + rtotal + '","' + todaystr1 + '","' + realtime(data.gamenum) + '");';
            query(sql1, function (err, vals, fields) {
                if (err) {
                    throw err
                }
                if (!vals.length) {
                    console.log(json_data[json_data.length - 1].gamenum + '期,数据库中不存在,准备写入');
                    console.log(json_data[json_data.length - 1].gamenum + '期,球号为' + json_data[json_data.length - 1].r1, json_data[json_data.length - 1].r2, json_data[json_data.length - 1].r3, json_data[json_data.length - 1].r4, json_data[json_data.length - 1].r5, json_data[json_data.length - 1].r6, json_data[json_data.length - 1].r7, json_data[json_data.length - 1].r8, json_data[json_data.length - 1].r9, json_data[json_data.length - 1].r10, json_data[json_data.length - 1].r11, json_data[json_data.length - 1].r12, json_data[json_data.length - 1].r13, json_data[json_data.length - 1].r14, json_data[json_data.length - 1].r15, json_data[json_data.length - 1].r16, json_data[json_data.length - 1].r17, json_data[json_data.length - 1].r18, json_data[json_data.length - 1].r19, json_data[json_data.length - 1].r20);
                    query(sql2, function (err, vals, fields) {
                        if (err) {
                            throw err
                        }
                        console.log(json_data[json_data.length - 1].gamenum + '期,写入sql成功');
                    })
                } else {
                    console.log(json_data[json_data.length - 1].gamenum + '期,数据库中已存在,无需写入');
                }
            })
        } else {
            console.log('拿到的数据不正确,等待下一次尝试')
        }
    });
}


function init() {
    var time = new Date();
    console.log('——————————————————分割线——————————————————');
    console.log('抓取时间:' + time);
    pk10day();
}

init();
setInterval(init, 20000);


process.on('uncaughtException', function (err) {
    console.log(err.stack);
});
